<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/20 9:20
 * Modified Time: 2019/7/20 9:20
 * Version: 2.0
 */

namespace App\Modules\System\Controllers;


use App\Lib\Utilities;
use App\Modules\Employee\Services\EmployeeService;
use App\Modules\Employee\Transformers\EmployeeTransformer;
use App\Modules\System\Services\CommonService;
use App\Modules\System\Services\ShareService;
use Flower\Core\Http\BaseController;
use Illuminate\Http\Request;

class CommonController extends BaseController
{
    private $commonService;
    private $employeeService;

    public function __construct(CommonService $commonService, EmployeeService $employeeService)
    {
        $this->commonService = $commonService;
        $this->employeeService = $employeeService;
    }

    public function getRules()
    {
        return [
            'sendSms' => [
                'mobile' => [
                    'title' => '手机号码',
                    'rule' => 'required',
                    'message' => []
                ],
                'scene' => [
                    'title' => '场景值',
                    'rule' => 'required',
                    'message' => []
                ]
            ],
            'checkSmsCode' => [
                'phone' => [
                    'title' => '手机号码',
                    'rule' => 'required',
                    'message' => []
                ],
                'scene' => [
                    'title' => '场景值',
                    'rule' => 'required',
                    'message' => []
                ],
                'code' => [
                    'title' => '验证码',
                    'rule' => 'required',
                    'message' => []
                ],
            ]
        ];
    }

    //发送短信
    public function sendSms(Request $request)
    {
        $mobile = $request->input('mobile');
        $scene = $request->input('scene');

        $this->commonService->sendSms($mobile, $scene);

        return $this->success('发送成功');
    }


    /**
     * 用户登录
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\NotFoundException
     */
    public function login(Request $request)
    {
        $mobile = $request->mobile;
        $password = $request->password;
        //用户登录返回用户详细信息
        $userInfo = $this->employeeService->loginEmployee($mobile, $password);
        if (empty($userInfo)) return $this->error("登录失败！");
        $roleInfo = $this->employeeService->getRoleInfo($userInfo->id);
        return $this->display((new EmployeeTransformer($userInfo, $roleInfo))->transform());
    }


    /**
     * 忘记密码
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\NotFoundException
     */
    public function forgotPasswordUser(Request $request)
    {
        $isPass = $this->commonService->checkSmsCode($request->mobile, 3, $request->captcha);
        if (!$isPass) return $this->error("验证码错误");

        $userInfo = $this->employeeService->changePasswordUser($request->mobile, $request->new_password);
        $roleInfo = $this->employeeService->getRoleInfo($userInfo->id);

        return $this->display((new EmployeeTransformer($userInfo, $roleInfo))->transform());
    }

    /**
     * 获取角色列表
     * @return mixed
     */
    public function getRoleList()
    {
        $list = $this->commonService->getRoleList();
        return $this->display($list);
    }


}